package com.petrochina.cloudconfig.controller;


import com.petrochina.cloudconfig.biz.EnvBiz;
import com.petrochina.cloudconfig.domain.Env;
import com.petrochina.cloudconfig.domain.SysInfo;
import com.petrochina.cloudconfig.util.EnvUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/env")
public class EnvController {

    @Autowired
    private EnvBiz envBiz;



    @RequestMapping(value="",method = RequestMethod.GET)
    public List<Env> list(@RequestParam(required = false) String keyWord){

        return envBiz.list(keyWord);
    }

    @RequestMapping(value = "/{envFullName:.+}",method = RequestMethod.GET)
    public void getEnv(HttpServletResponse response,@PathVariable String envFullName){
        if(EnvUtil.isEnvFullName(envFullName))
        envBiz.getEnv(response,envFullName);
    }

    @RequestMapping(value="",method = RequestMethod.POST)
    public SysInfo addEnv(@RequestParam("envFullName") String envFullName, @RequestParam("file") MultipartFile file){

        SysInfo sysInfo = new SysInfo();
        //TODO check file is json
        envBiz.addEnv(envFullName,file);

        return sysInfo;

    }

    @RequestMapping(value = "/{envFullName:.+}",method = RequestMethod.DELETE)
    public SysInfo delEnv(@PathVariable String envFullName){

        SysInfo sysInfo = new SysInfo();
        envBiz.delEnv(envFullName);

        return sysInfo;
    }

















}
